package org.yx.service.smat.merge_material_sn;

import org.yx.exception.BizException;
import org.yx.models.SmatMsnSts;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryIn;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryService;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;
import org.yx.utils.ServiceBeanUtil;

import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 物料批合并
 * 
 * @author zxw
 * @since 2019/04/17
 **/
public class SmatMergeMaterialSnService extends BaseService {
	public SmatMergeMaterialSnOut mergeMaterialSn(SmatMergeMaterialSnIn in) throws Exception {
		checkStringNotBlank(in, "fromMsn,toMsn");

		if (in.getMoveQty() == null || in.getMoveQty().doubleValue() < 0.0001d) {
			throw new BizException("SYS-0005", "请求的参数${parameter}要求的值${value}不正确。",
					FieldMap.of("parameter", "moveQty"), FieldMap.of("value", in.getMoveQty()));
		}

		if (StrUtil.equals(in.getFromMsn(), in.getToMsn())) {
			throw new BizException("SMAT-0010", "两物料批次相同。");
		}

		SmatMsnSts fromMsnSts = selectOneLocked(SmatMsnSts.class, in.getUserOrgCode(), in.getFromMsn());
		if (fromMsnSts == null || "D".equals(fromMsnSts.getStatus())) {
			throw new BizException("SMAT-0005", "物料批号${msn}不存在或已经被删除。", FieldMap.of("msn", in.getFromMsn()));
		}
		if ("H".equals(fromMsnSts.getStatus())) {
			throw new BizException("SMAT-0006", "物料批号${msn}已经被锁定，锁定单号${holdNo}。", FieldMap.of("msn",
					fromMsnSts.getMsn()), FieldMap.of("holdNo", fromMsnSts.getHoldNo()));
		}
		if (in.getMoveQty().compareTo(fromMsnSts.getQty()) > 0) {
			throw new BizException("SMAT-0007", "转移数量${moveQty}超出可用数量${qty}。", FieldMap.of("moveQty", in.getMoveQty()),
					FieldMap.of("qty", fromMsnSts.getQty()));
		}

		SmatMsnSts toMsnSts = selectOneLocked(SmatMsnSts.class, in.getUserOrgCode(), in.getToMsn());
		if (toMsnSts == null || "D".equals(toMsnSts.getStatus())) {
			throw new BizException("SMAT-0005", "物料批号${msn}不存在或已经被删除。", FieldMap.of("msn", in.getToMsn()));
		}
		if ("H".equals(toMsnSts.getStatus())) {
			throw new BizException("SMAT-0006", "物料批号${msn}已经被锁定，锁定单号${holdNo}。",
					FieldMap.of("msn", toMsnSts.getMsn()), FieldMap.of("holdNo", toMsnSts.getHoldNo()));
		}
		if (!StrUtil.equals(fromMsnSts.getWhCode(), toMsnSts.getWhCode())) {
			throw new BizException("SMAT-0008", "正在合并的物料批号不是同一仓库。");
		}
		if (!StrUtil.equals(fromMsnSts.getMatCode(), toMsnSts.getMatCode())
				|| !StrUtil.equals(fromMsnSts.getMatVer(), toMsnSts.getMatVer())) {
			throw new BizException("SMAT-0009", "正在合并的物料批号不是同一物料编码。");
		}

		fromMsnSts.setQty(NumberUtil.sub(fromMsnSts.getQty(), in.getMoveQty()));
		fromMsnSts.setDtlSeq(fromMsnSts.getDtlSeq() + 1);
		fromMsnSts.setEventCode("MERGE");
		fromMsnSts.setEventValue(toMsnSts.getMsn());
		fromMsnSts.setMergeToMsn(toMsnSts.getMsn());
		update(fromMsnSts, "qty,dtlSeq,eventCode,eventValue,mergeToMsn");
		SmatInsertMaterialSnHistoryIn snHisIn = new SmatInsertMaterialSnHistoryIn();
		snHisIn.setMsnSts(fromMsnSts);
		ServiceBeanUtil.getBean(SmatInsertMaterialSnHistoryService.class).insertMaterialSnHistory(snHisIn);

		toMsnSts.setQty(NumberUtil.add(toMsnSts.getQty(), in.getMoveQty()));
		toMsnSts.setDtlSeq(toMsnSts.getDtlSeq() + 1);
		toMsnSts.setEventCode("MERGE");
		toMsnSts.setEventValue(fromMsnSts.getMsn());
		toMsnSts.setSplitFromMsn(fromMsnSts.getMsn());
		update(toMsnSts, "qty,dtlSeq,eventCode,eventValue,splitFromMsn");
		snHisIn.setMsnSts(toMsnSts);
		ServiceBeanUtil.getBean(SmatInsertMaterialSnHistoryService.class).insertMaterialSnHistory(snHisIn);

		SmatMergeMaterialSnOut out = new SmatMergeMaterialSnOut();

		return out;
	}
}